package com.xh.petdogexpert.expert.service.imp;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xh.petdogexpert.base.BaseException;
import com.xh.petdogexpert.expert.dto.Disease;
import com.xh.petdogexpert.expert.dto.Symptom;
import com.xh.petdogexpert.expert.mapper.DiseaseMapper;
import com.xh.petdogexpert.expert.service.IDiseaseService;

@Service
public class DiseaseServiceImp implements IDiseaseService {

	@Autowired
	DiseaseMapper diseaseMapper;

	Logger logger = LoggerFactory.getLogger(DiseaseServiceImp.class);

	/**
	 * 初步提交症状后返回其他症状给用户选择 只获取前面5个疾病
	 * 
	 * @author linxixin
	 * @date 2016年3月23日
	 * @see com.xh.petdogexpert.expert.service.ISymptomService#getDiseaseSumCf(java.util.List)
	 */
	@Override
	public List<Disease> getDiseaseSumCf(List<String> symptomIds) {

		List<Disease> diseaseSumCf = diseaseMapper.getDiseaseSumCf(symptomIds);
		if (diseaseSumCf.size() > 5) {
			diseaseSumCf = diseaseSumCf.subList(0, 5);
		}
		
		if (logger.isDebugEnabled()) {
			logger.debug(diseaseSumCf.size() + "  !!");
			for (Disease s : diseaseSumCf) {
				logger.debug(s.getId() + "  " + s.getSumCf() + "  !!");
			}
		}

		return diseaseSumCf;
	}
	
	/**
	 * 获取详细的疾病信息
	 * @author linxixin
	 * @date 2016年3月24日
	 * @see com.xh.petdogexpert.expert.service.IDiseaseService#getDiseaseBaseInfo(java.util.List)
	 */
	public List<Disease> getDiseaseBaseInfo (List<Disease> diseases) {
		
		List<Disease> detail = diseaseMapper.getDiseaseBaseInfo(diseases);
		
		//在获取的详细疾病里面增加旧disease的sumCf
		for(Disease i : detail) {
			for(Disease j : diseases) {
				if(i.getId().equals(j.getId())) {
					i.setSumCf(j.getSumCf());
					break;
				}
			}
		}
		
		return detail;
	}
	@Override
	public List<Disease> queryDisease(Disease disease) {
		return diseaseMapper.queryDisease(disease);	
	}

	

	@Override
	public int addUpdateDisease(Disease disease) throws BaseException {
		// TODO Auto-generated method stub
		

		try {
			if (disease.getId() != null)
				return diseaseMapper.updateByPrimaryKeySelective(disease);
			else
				return diseaseMapper.addDisease(disease);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BaseException("增加症状出错");
		}
	}
	
	@Override
	public Disease getDisease(Disease disease) throws BaseException {
		List<Disease> diseases = diseaseMapper.queryDisease(disease);
		if (diseases.size() > 1) {
			throw new BaseException("获取多于一个疾病的数据");
		} else if (diseases == null || diseases.size() == 0) {
			throw new BaseException("不存在此疾病的数据");
		} else {
			return disease.get(0);
		}

	}
	@Override
	public int deleteDisease(Disease disease) throws BaseException {
		int count;
		try {
			count =diseaseMapper.deleteDisease(disease);

		} catch (Exception e) {
			e.printStackTrace();
			throw new BaseException("删除失败");
		}

		return count;
	}
}
